import os
from nlp_tools.corpus.ner.corpus_loader import ChineseDailyNerCorpus


model_type = 'bert'
bert_model_path = r'/home/qiufengfeng/nlp/pre_trained_model/chinese_roberta_wwm_ext_L-12_H-768_A-12'
bert_model_token_path = os.path.join(bert_model_path,'vocab.txt')

train_data = '/home/qiufengfeng/nlp/competition/天池/CCKS2021中文NLP地址要素解析/data/train.conll'
dev_data =  '/home/qiufengfeng/nlp/competition/天池/CCKS2021中文NLP地址要素解析/data/dev.conll'
test_data = r"/tcdata/final_test.txt"



submit_path = r"/result.txt"
model_save_path = '/home/qiufengfeng/nlp/competition/天池/CCKS2021中文NLP地址要素解析/tf_models'


use_network_type = "global_pointer"

save_endfix = ""

fianl_model_save_path = os.path.join(model_save_path,use_network_type+save_endfix)
#####
#数据加载器类
data_loader_class = ChineseDailyNerCorpus



from nlp_tools.processors import SequenceProcessor
from nlp_tools.processors.ner.global_pointer_label_processor import GlobalPointerLabelProcessor


from nlp_tools.tokenizer.bert_tokenizer import BertTokenizer
from nlp_tools.embeddings import BertEmbedding


from nlp_tools.tasks.labeling.global_point_model import GlobalPointModel

from nlp_tools.generators import BatchGenerator

network_configs = {
    'global_pointer':{
        "Tokenizer":{
                    'class':BertTokenizer,
                    'params':{"token_dict":bert_model_token_path,"do_lower_case":True,"simplified":True}
        },

        "Embedding":{
            'class':BertEmbedding,
            'params':{"model_folder":bert_model_path}
        },

        "SentenceProcessor":{
            "class":SequenceProcessor,
            "params":{
                'text_tokenizer':{}
            }
        },

        "LabelProcessor": {
            'class':GlobalPointerLabelProcessor,
            'params':{}
        },

        "Network":{
            'class':GlobalPointModel,
            'params':{
                'use_rdrop':False,
                'use_FGM':False,
            }
        }
    }
}

train_params = {
    'data_loader':ChineseDailyNerCorpus,
    'train_data':train_data,
    'dev_data':dev_data,

    'model_save_path':model_save_path,
    'fit_params':{
        'epochs':15,
        'batch_size':50,
        'generator':BatchGenerator
    }
}



def init_class(class_info):
    class_type = class_info['class']
    class_params = class_info['params']
    return class_type(**class_params)

def init_object(config_dict,object_name,object_params_init=None):
    if object_name not in config_dict:
        raise ValueError("传入的参数有问题")

    object_dict = config_dict[object_name]
    if 'class' not in object_dict:
        raise ValueError("传入的参数有问题")
    object_class = object_dict['class']

    object_params = {}
    if 'params' in object_dict:
        object_params = object_dict['params']

    if  object_params_init:

        object_params.update(object_params_init)

    for key,value in object_params.items():
        if type(value) == dict:
            object_params[key] = init_class(value)


    return object_class(**object_params)




